package com.djsearch.ui;


import org.json.JSONException;

import com.djsearch.R;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.api.*;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 分享商品到我的微薄
 * @author chenyang2
 *
 */

public class ShareToWeiboActivity extends BaseActivity{
	
	private Context mContext;
	private String shareContent,goodsUrl;
	private EditText shareContentEdit;
	private Button backBtn;
	private TextView shareBtn;
	private CheckBox bindSina,bindTencent;
	private TextView tongbuSina,tongbuTencent;
	private boolean isBindSina = false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.share_toweibo_layout);
		mContext = this;
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();              
		shareContent = extras.getString("shareContent");
		goodsUrl = extras.getString("goodsUrl");
		initView();
		initOnClick();
	}
	@Override
	void initHandler() {
		// TODO Auto-generated method stub
		
	}
	@Override
	void initView() {
		// TODO Auto-generated method stub
		shareContentEdit = (EditText)findViewById(R.id.shareEidt);
		shareContentEdit.setText(shareContent);
		backBtn = (Button)findViewById(R.id.btnBack);
		shareBtn = (TextView)findViewById(R.id.sendText);
		bindSina = (CheckBox)findViewById(R.id.issina);
		if (preferences.getString("tongbuSina", "no").equals("yes")) {
			bindSina.setChecked(true);
			bindSina.setButtonDrawable(R.drawable.checkbox_selected);
		} else {
			bindSina.setButtonDrawable(R.drawable.checkbox_normal);
			bindSina.setChecked(false);
		}
		bindTencent = (CheckBox)findViewById(R.id.istencent);
		tongbuSina = (TextView)findViewById(R.id.tongbusina);
		tongbuTencent = (TextView)findViewById(R.id.tongbutencent);
	}
	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		bindSina.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					//先判断是否绑定新浪微博，如果没有绑定那么提示去绑定，或者不能将分享同步到新浪微博，该逻辑待处理
					if (!isBindSina()) {
						Toast.makeText(mContext, "你未绑定微博，不能同步", 5000).show();
					} else {
						bindSina.setButtonDrawable(R.drawable.checkbox_selected);
						tongbuSina.setText("已同步");
						SharedPreferences.Editor editor = preferences.edit();
						editor.putString("tongbuSina", "yes").commit();
					}
				} else {
					bindSina.setButtonDrawable(R.drawable.checkbox_normal);
					tongbuSina.setText("未同步");
					SharedPreferences.Editor editor = preferences.edit();
					editor.putString("tongbuSina", "no").commit();
				}
			}
		});
		bindTencent.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					bindTencent.setButtonDrawable(R.drawable.checkbox_selected);
					tongbuTencent.setText("已同步");
				} else {
					bindTencent.setButtonDrawable(R.drawable.checkbox_normal);
					tongbuTencent.setText("未同步");
				}
			}
		});
		shareBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				requestShareData();
			}
		});
	}
	
	public void shareContentToTecentWeibo(String content) {
//		String clientId = preferences.getString("clientId", ""); //801340347  1130fbac25f04642b506bcd9e518ed49 http://www.dianjinshou.cn
//		String clientSecret = preferences.getString("clientSecret", "");
//		String redirectUri = preferences.getString("redirectUri", "");
//		OAuthV2 oAuth = new OAuthV2(clientId, clientSecret, redirectUri);
//		TAPI tAPI = new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
//		try {
//			tAPI.add(oAuth, "json", content, "");
//		} catch (Exception e) {
//			// TODO: handle exception
//		} finally {
//			tAPI.shutdownConnection();
//		}
		SetActivity.share(content);
		
	}
	
	
	private void requestShareData() {
		IRequest request = new JSONReq("share");
		request.putParam("userId", preferences.getString("userId", ""));
		request.putParam("content",shareContentEdit.getText().toString());
		request.putParam("url", goodsUrl);
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onStart() {
				showDialog("正在提交分享...");
				if (bindSina.isChecked()) {
					shareContentToSinaWeibo(shareContentEdit.getText().toString()+goodsUrl);
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							shareContentToTecentWeibo(shareContentEdit.getText().toString()+goodsUrl);
						}
					}).start();
//					shareContentToTecentWeibo(shareContentEdit.getText().toString()+goodsUrl);
				}
			}
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
				    try {
						Toast.makeText(mContext, response.getString("message"), 5000).show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					getDataToResolve(response);
				} else if (IResponse.Fail==result) {
					try {
						Toast.makeText(mContext, response.getString("message"), 5000).show();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					missDialog();
			}
			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
			}
			
		});
	
		
	
	}
	
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		
	}

	
}
